import { Input, message, Space } from 'antd'
import { memo } from 'react'
import { Link, useNavigate } from 'react-router-dom'
import { register } from '@/service/module/user'
import styles from './Register.module.scss'
import { Typography, Form, Button } from 'antd'
import { UserAddOutlined } from '@ant-design/icons'
import { useRequest } from 'ahooks'
import { useForm } from 'antd/es/form/Form'
const { Title } = Typography

const Register = memo(() => {
  const [form] = useForm()

  const nav = useNavigate()

  // 注册
  const onFinish = (values: any) => {
    run(values)
  }

  const { run } = useRequest(
    async ({ username, password, nickname }) => {
      const data = await register(username, password, nickname)
      return data
    },
    {
      manual: true,
      onSuccess() {
        message.success('注册成功')
        form.resetFields()
        nav('/login')
      },
    }
  )

  return (
    <div className={styles.container}>
      <div>
        <Space>
          <Title level={2}>
            <UserAddOutlined />
          </Title>
          <Title level={2}>用户注册</Title>
        </Space>
      </div>
      <div className="form">
        <Form
          form={form}
          labelCol={{ span: 8 }}
          wrapperCol={{ span: 16 }}
          onFinish={onFinish}
          autoComplete="off"
        >
          <Form.Item
            label="用户名"
            name="username"
            rules={[
              {
                required: true,
                message: '请输入用户名~',
              },
              {
                type: 'string',
                min: 5,
                max: 20,
                message: '字符长度在 5-20 之间',
              },
              {
                pattern: /^\w+$/,
                message: '只能是字母数字下划线',
              },
            ]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="密码"
            name="password"
            rules={[
              {
                required: true,
                message: '请输入密码',
              },
            ]}
          >
            <Input.Password />
          </Form.Item>
          <Form.Item
            label="确认密码"
            name="confirm"
            dependencies={['password']}
            rules={[
              { required: true },
              ({ getFieldValue }) => ({
                validator(_, value) {
                  if (!value || getFieldValue('password') === value) {
                    return Promise.resolve()
                  }
                  return Promise.reject(new Error('两次密码不一致!'))
                },
              }),
            ]}
          >
            <Input.Password />
          </Form.Item>
          <Form.Item label="昵称" name="nickname">
            <Input />
          </Form.Item>
          <Form.Item label={null}>
            <Space>
              <Button type="primary" htmlType="submit">
                注册
              </Button>
              <Link to="/login">已有帐号 去登录</Link>
            </Space>
          </Form.Item>
        </Form>
      </div>
    </div>
  )
})

export default Register
